Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform two requests concurrently and combine their results #98

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

sharplet
Copy link
Contributor

Adds a new extension to the Client protocol that allows two requests to be performed concurrently, with their results combined into a tuple:

perform(_: RequestA, _: RequestB, completionHandler: (Result<(ResponseA, ResponseB), SwishError>) -> Void)

If either fails, the other request is cancelled and the failure result is passed to the completion handler. A new Cancelable protocol allows cancelable things to be composed together (e.g., dispatch blocks and URL session tasks).

I plan to add some tests for the basic behaviours, especially around how cancellation is handled. I honestly am not sure how to write a test for the thread-safety aspects of this code, but I'm pretty sure all reads and writes to the combined state are appropriately synchronised. I'll do another review to make sure there's no race between updating the combined state and waiting on the request group to complete.

@sharplet
Copy link
Contributor Author

I also had to update the macOS deployment target to 10.10 to make use of DispatchWorkItem.

private var state: State
private var requests = CompositeCancelable()
private let group = DispatchGroup()
private let queue = DispatchQueue(label: "com.movetogive.MTGClient.RequestGroup<\(State.self)>")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, accidentally let this slip in

Adds a new method to the `Swish.Client` protocol, allowing two requests
to be performed concurrently and combining their results in a
thread-safe manner.

If either request fails with an error, the other is cancelled if it is
still in flight. The requests are performed on a global concurrent queue
of default priority.
Exposes `scheduler` as a public property of the `Client` protocol, to
allow extensions to make use of it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant